home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / util / wb / wbstars2.lha / wbstars2.0 / source / WBStars_main.c < prev    next >
C/C++ Source or Header  |  1998-01-18  |  4KB  |  188 lines

  1. /* $VER: WBStars_main.c 2.0b5 (18 Jan 1998)
  2. */
  3.  
  4. #include <intuition/screens.h>
  5. #include <proto/intuition.h>
  6. #include <dos/dos.h>
  7. #include <proto/exec.h>
  8. #include <proto/dos.h>
  9. #include <proto/intuition.h>
  10. #include <proto/graphics.h>
  11. #include <clib/alib_protos.h>
  12. #include <clib/macros.h>
  13. #include <math.h>
  14. #include <string.h>
  15.  
  16. #include "WBStars_include.h"
  17. #include "WBStars_protos.h"
  18. #include "WBStars_plot.h"
  19.  
  20. struct Screen        *wbench=NULL;
  21. struct RastPort        *RPort;
  22. struct Layer_Info    *LInfo;
  23. struct ColorMap        *cm;
  24. char            pen_obtained;
  25. int            bgcolor;
  26.  
  27. long    _stack=4000;
  28. char    *_procname="WBStars";
  29. long    _priority=0;
  30. long    _BackGroundIO=FALSE;
  31.  
  32. int        minx,miny,width,height;
  33. BYTE    CXpri=0;
  34. UBYTE    *hotkey;
  35. char    activated=FALSE;
  36. char    locked=FALSE;
  37. char    version[]="$VER: WBStars 2.0b5 (18 Jan 1998)\0";
  38. int    bg_color;
  39. int    fg_color;
  40. int    Loop_Wait;
  41. int    Max_Objects;
  42. int    Max_Stick;
  43. int    Check_Stick;
  44. int    V_Flutter;
  45. char    Planes;
  46. char    Remember;
  47. char    Force;
  48. int    Wind;
  49. int    few_Flakes;
  50.  
  51. void    main(long argc, UBYTE **argv)
  52. {
  53.     UBYTE    **ttypes;
  54.     struct EasyStruct    fail1={sizeof(struct EasyStruct),0,"WBStars","failed to open libraries"," Shit ! "};
  55.     int    delay;
  56.  
  57.     if( OpenLibraries() )
  58.     {
  59.         /* read the Tooltypes */
  60.         ttypes        = ArgArrayInit(argc, argv);
  61.         delay        = ArgInt(ttypes, "INITIAL_DELAY", 0);
  62.         CXpri        = (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
  63.         hotkey        = ArgString(ttypes, "CX_POPKEY", "control alt w");
  64.         bgcolor        = ArgInt(ttypes, "BG_COLOR", -1);
  65.         Loop_Wait    = ArgInt(ttypes, "LOOP_WAIT", 3);
  66.         Max_Objects    = ArgInt(ttypes, "MAX_OBJECTS", 1000);
  67.         Max_Stick    = ArgInt(ttypes, "MAX_STICK", 100);
  68.         V_Flutter    = MAX(MIN(ArgInt(ttypes, "V_FLUTTER", 20),100),0);
  69.         Planes        = MAX(MIN(ArgInt(ttypes, "PLANES", 1),15),1);
  70.         few_Flakes    = MAX(MIN(ArgInt(ttypes, "FEW_FLAKES", 1),32767),1);
  71.         Remember    = (char)(0!=strcmpi(ArgString(ttypes, "REMEMBER", "NO"),"NO\0"));
  72.         Force        = (char)(0!=strcmpi(ArgString(ttypes, "FORCE", "NO"),"NO\0"));
  73.         Wind        = ArgInt(ttypes, "WIND", 0);
  74.         Check_Stick    = ArgInt(ttypes, "CHECK_STICK", 5);
  75.         SetTaskPri(FindTask(0),MAX(MIN(ArgInt(ttypes, "TOOLPRI", 0),127),-128));
  76.         ArgArrayDone();
  77.  
  78.         if( InitCx() )
  79.         {
  80.             Delay(delay);
  81.             while( HandleCx() )
  82.             {
  83.                 if(activated) PlotObjects();
  84.  
  85.                 Delay(Loop_Wait);
  86.             }
  87.             RemovCx();
  88.         }
  89.     }
  90.     else
  91.     {
  92.         if( IntuitionBase ) EasyRequestArgs(NULL,&fail1,0,NULL);
  93.     }
  94.     CloseLibraries();
  95.     if( activated )    Inactivate();
  96. }
  97.  
  98.  
  99.  
  100.  
  101. void    Inactivate()
  102. {
  103.     if(activated)
  104.     {
  105.         ClearObjects();
  106.         if(pen_obtained)ReleasePen(cm,fg_color);
  107.         UnlockPubScreen( NULL, wbench);
  108.         wbench=NULL;
  109.         activated=FALSE;
  110.     }
  111. }
  112.  
  113. char    Activate()        /* is called, if WBStars should activate */
  114. {                /* returns TRUE if Workbench could be locked */
  115.     short    i;
  116.     short    testcolor[256];
  117.     short    h;
  118.     double    dist;
  119.     double    mindist=-1.0;
  120.     ULONG    color;
  121.     short    ncolors;
  122.     short    r,g,b;
  123.  
  124.     if( activated ) Inactivate();    /* if activated twice inactivate before */
  125.                                         /* locking workbench again */
  126.     if( wbench=LockPubScreen("Workbench") )
  127.     {
  128.         RPort        = &(wbench->RastPort);    /* the WBench´s RPort is the */
  129.         LInfo        = &(wbench->LayerInfo);    /* one we want to draw in */
  130.         minx        = wbench->LeftEdge;
  131.         miny        = wbench->TopEdge + wbench->BarHeight + 1;
  132.         width        = wbench->Width;
  133.         height        = wbench->Height - wbench->BarHeight - 1;
  134.         cm        = wbench->ViewPort.ColorMap;
  135.         ncolors        = (short)(1<<(RPort->BitMap->Depth));
  136.  
  137.         bg_color    =bgcolor;
  138.  
  139.         if( bg_color<0 )
  140.         {
  141.             for(i=0;i<(short)(1<<(RPort->BitMap->Depth));i++)
  142.             {
  143.                 testcolor[i]=0;
  144.             }
  145.             for(i=0;i<width;i++)
  146.             {
  147.                 testcolor[ReadPixel(RPort,i+minx,miny)]++;
  148.             }
  149.             bg_color=0;
  150.             h=testcolor[0];
  151.             for(i=1;i<(short)(1<<(RPort->BitMap->Depth));i++)
  152.             {
  153.                 if(testcolor[i]>h)
  154.                 {
  155.                     bg_color=i;
  156.                     h=testcolor[i];
  157.                 }
  158.             }
  159.         }
  160.         if(OS3)
  161.         {
  162.             fg_color=(int)ObtainBestPenA(cm,(ULONG)0xFFFF0000,(ULONG)0xFFFF0000,(ULONG)0xFFFF0000,NULL);
  163.             pen_obtained=TRUE;
  164.         }
  165.         if(!OS3 || fg_color==-1)
  166.         {
  167.             for(i=0;i<ncolors;i++)
  168.             {
  169.                 color=GetRGB4(cm,(long)i);
  170.                 r=(color>>8)&15;
  171.                 g=(color>>4)&15;
  172.                 b=color&15;
  173.  
  174.                 if( (i!=bg_color) && (((dist=pow((double)(15-r),2.0)+pow((double)(15-g),2.0)+pow((double)(15-b),2.0))<mindist)||(mindist<0.0)) )
  175.                 {
  176.                     mindist=dist;
  177.                     fg_color=i;
  178.                 }
  179.             }
  180.             pen_obtained=FALSE;
  181.         }
  182.  
  183.         activated=TRUE;
  184.         InitObjects();
  185.     }
  186.  
  187.     return activated;
  188. }